package com.itheima.dao;

import com.itheima.domain.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/*
* 角色
* */
public interface RoleDao {

    /*根据用户id查询角色信息*/
    @Select("select * from role where id in (select roleId from users_role where userId = #{id})")
    List<Role> findById(String id) throws Exception;

    /*查询所有角色*/
    @Select("select * from role")
    List<Role> findAll() throws Exception;

    /* 添加角色信息*/
    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void save(Role role) throws Exception;

    // 根据用户id查询没有添加的角色
    @Select("select * from role where id not in(select roleId from users_role where userId = #{id})")
    List<Role> findOtherRole(String id) throws Exception;

    // 根据角色id查询角色信息
    @Select("select * from role where id = #{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roleName",column = "roleName"),
            @Result(property = "roleDesc",column = "roleDesc"),
            @Result(property = "permissions",column = "id",javaType = java.util.List.class,many = @Many(select = "com.itheima.dao.PermissionDao.findById"))
    })
    Role findByRoleId(String id) throws Exception;

    //添加角色和资源权限的关联
    @Insert("insert into role_permission(roleId,permissionId) values(#{roleId},#{permissionId})")
    void addPermissionToRole(@Param("roleId") String roleId, @Param("permissionId") String permissionId)throws Exception;
}
